package org.example.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.common.Result;
import org.example.entity.Notice;
import org.example.service.NoticeService;
import org.example.service.ReviewService;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    NoticeService noticeService;


    //    新增用户信息
    @PostMapping("/add")
    public Result add(@RequestBody Notice notice){
        notice.setTime(DateUtil.now());
        noticeService.save(notice);
        return Result.success();
    }
    //修改用户信息
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice){
        noticeService.updateById(notice);
        return Result.success();
    }
    //删除用户
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        noticeService.removeById(id);
        return Result.success();
    }
    //批量删除用户
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){
        noticeService.removeByIds(ids);
        return Result.success();
    }
    //查询全部用户信息
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Notice> noticeList = noticeService.list(new QueryWrapper<Notice>().orderByDesc("id"));  //select * from user order by id desc
        return Result.success(noticeList);
    }

//    //根据ID查询用户信息
//    @GetMapping("/selectById/{id}")
//    public Result selectById(@PathVariable Integer id){
//        Notice notice= noticeService.getById(id);
//        return Result.success(notice);
//    }
    //查询全部用户信息
    @GetMapping("/selectData")
    public Result selectUserData(){
        QueryWrapper<Notice> queryWrapper =new QueryWrapper<Notice>().orderByDesc("id");
        queryWrapper.eq("open",1);
        List<Notice> noticeList = noticeService.list(queryWrapper);  //select * from user order by id desc
        return Result.success(noticeList);
    }

    //多条件分页模糊查询用户信息
    @GetMapping("/selectByPage")
    public Result selectByPage( @RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam String title
                                ){
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<Notice>().orderByDesc("id");
        queryWrapper.like(StrUtil.isNotBlank(title), "title" ,title);
        // select *from user where username like '%#{username}%' and name like '%#{name}%'
        Page<Notice> page = noticeService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
}